package com.example.myapplication.User;

import static android.content.ContentValues.TAG;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.NetworkOnMainThreadException;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.example.myapplication.AbsFragment.Home;
import com.example.myapplication.Data.UserData;
import com.example.myapplication.LoginRegister.LoginActivity;
import com.example.myapplication.R;
import com.example.myapplication.WeActivity;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class UserPersonal extends Home {
    private Context mContext;
    private UserData userData;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_user_personal;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mContext = context;
    }

    public void get(){
        new Thread(() -> {
            userData = (UserData)getActivity().getIntent().getSerializableExtra("userData");
            int id = userData.getId();
            // url路径
            String url = "http://47.107.52.7:88/member/sign/course/student?"
                    +"userId="+id;

            // 请求头
            Headers headers = new Headers.Builder()
                    .add("appId", "255cb403024d44f692620834ffc73623")
                    .add("appSecret", "12472e74638ef69e7412dbf2e818c2ae538d4")
                    .add("Accept", "application/json, text/plain, */*")
                    .build();

            //请求组合创建
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .get()
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                //发起请求，传入callback进行回调
                client.newCall(request).enqueue(callback);
            }catch (NetworkOnMainThreadException ex){
                ex.printStackTrace();
            }
        }).start();
    }

    private final Callback callback = new Callback() {
        @Override
        public void onFailure(@NonNull Call call, IOException e) {
            //TODO 请求失败处理
            e.printStackTrace();
        }
        @Override
        public void onResponse(@NonNull Call call, Response response) throws IOException {
            //TODO 请求成功处理
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    initData();
                }
            });
        }
    };

    public void initData(){
        ImageView iv_avatar = (ImageView) getActivity().findViewById(R.id.iv_avatar);
        TextView tv_real_name=(TextView) getActivity().findViewById(R.id.tv_real_name);
        TextView tv_id_number=(TextView) getActivity().findViewById(R.id.tv_id_number);
        
        if (iv_avatar == null) {
            Log.e(TAG, "ImageView iv_avatar is null");
            return;
        }

        if(userData.getAvatar()!=null){
            Glide.with(this)
                    .load(userData.getAvatar())//图片网址
                    .circleCrop()
                    .into(iv_avatar);
        }
        tv_real_name.setText(userData.getRealName());
        tv_id_number.setText(String.valueOf(userData.getIdNumber()));

        RelativeLayout myRelativeLayout1 = getActivity().findViewById(R.id.re_aboutwe);
        myRelativeLayout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), WeActivity.class);
                startActivity(intent);
            }
        });

        // 退出登录事件
        RelativeLayout myRelativeLayout = getActivity().findViewById(R.id.tuichu);
        myRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 退出登录确定弹窗
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                // 设置对话框标题和消息
                builder.setTitle("警告")
                        .setMessage("是否确认退出登录。");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();// 关闭对话框
                        getActivity().finish();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

                // 创建并显示 AlertDialog
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
        RelativeLayout userRelativeLayout = getActivity().findViewById(R.id.re_xiangce);
        userRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(),UserMessageActivity.class);

                intent.putExtra("userData", userData);
                startActivity(intent);
            }
        });

    }
}
